/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFParaManager
  \brief a singleton manager class to manage all kinds of parameters globally,
  typically channel id maps, detector positions, calibration constants, and other
  miscellaneous ones.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-12-03
  \date 2021-12-04 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-12-03   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <unistd.h>
#include "ETFParaManager.h"
#include "ETFYaml.h"
#include "ETFMsg.h"

ETFParaManager *ETFParaManager::fInstance = nullptr;

ETFParaManager::ETFParaManager() : fChIdConfig(nullptr), fDetPosConfig(nullptr),
  fDelayConfig(nullptr), fMisConfig(nullptr){}

ETFParaManager::~ETFParaManager(){
  if(fChIdConfig) delete fChIdConfig;
  if(fDetPosConfig) delete fDetPosConfig;
  if(fDelayConfig) delete fDelayConfig;
  if(fMisConfig) delete fMisConfig;
} // end dtor

// check if p is null, and issue a warning if so
const ETFYaml *ETFParaManager::testnull(const ETFYaml *p) const{
  if(!p) ETFMsg::Error("ETFParaManager", "testnull: input pointer is nullptr");
  return p;
} // end member function testnull

ETFParaManager *ETFParaManager::Instance(){
  if(!fInstance) fInstance = new ETFParaManager();
  return fInstance;
} // end member function Instance

void ETFParaManager::SetConfigFile(ETFYaml *&c, const string &file){
  string f = getenv("VMCWORKDIR"); f += "/config/"; f += file;
  if(c) ETFMsg::Error("ETFParaManager", "SetConfigFile: input yaml not nullptr");
  if(access(f.c_str(), F_OK))
    ETFMsg::Error("ETFParaManager", "SetConfigFile: file %s non-existent", f.c_str());
  c = new ETFYaml(f);
} // end member function SetConfigFile
